package com.xnxy.design.action;


import com.xnxy.design.model.User;
import com.xnxy.design.model.type.OperateType;
import com.xnxy.design.service.CoreService;
import com.xnxy.design.util.ConstantClassFunction;
import com.xnxy.design.util.ControllerReturn;
import com.xnxy.design.util.PageData;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * controller层工具方法
 *
 * @author puzengcheng
 * @date 2018/10/12
 */
public abstract class CoreController<T, M> {


    /**
     * 初始化页码
     */
    private Integer initPageNum = 1;
    /**
     * 初始化每页条数
     */
    private Integer initPageSize = 10;


    public abstract CoreService<T, M> getService();


    /**
     * 过滤查询条件内容
     *
     * @param request
     * @return
     */
    public static List<String> getParameters(HttpServletRequest request) {
        Enumeration<String> paramNames = request.getParameterNames();
        List<String> result = new ArrayList<String>();
        while (paramNames != null && paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            if (paramName.startsWith(ConstantClassFunction.getPREFIX())) {
                String[] tmp = paramName.split("_");
                if (!StringUtils.isEmpty(request.getParameterValues(paramName)[0].toString())) {
                    if (tmp.length == 3) {
                        String str = tmp[1];
                        if (tmp[2].equals("LIKE")) {
                            str = str + " like concat('%','" + request.getParameter(paramName) + "','%')";
                        } else {
                            str = str + OperateType.getDesc(tmp[2]);
                            str = str + request.getParameter(paramName);
                        }
                        result.add(str);
                    }
                }
            }

        }
        return result;
    }


    /**
     * 调用返回map工具方法
     *
     * @param msg
     * @param data
     * @return Map
     */
    public static Map<String, Object> success(Object result, String msg, Object data) {
        return ControllerReturn.success(result, msg, data);
    }

    /**
     * 调用返回map工具方法
     *
     * @param msg
     * @return Map
     */
    public static Map<String, Object> success(Object result, String msg) {
        return ControllerReturn.success(result, msg);
    }

    /**
     * 查询工具方法
     *
     * @param request
     * @return
     */
    public PageData queryData(HttpServletRequest request) {
        Integer pageNum = getPagNum(request);
        Integer pageSize = getPagSize(request);
        if (pageNum == null) {
            pageNum = initPageNum;
        }
        if (pageSize == null) {
            pageSize = initPageSize;
        }
        try {
            PageData pageData = getService().queryData(pageNum, pageSize, getParameters(request));
            return pageData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前页
     *
     * @param request
     * @return
     */
    public int getPagNum(HttpServletRequest request) {
        String pageReq = request.getParameter("pageNum");
        if (null != pageReq) {
            return Integer.valueOf(pageReq);
        } else {
            return 1;
        }
    }

    /**
     * 获取当前页显示范围
     *
     * @param request
     * @return
     */
    public int getPagSize(HttpServletRequest request) {
        String pageReq = request.getParameter("pageSize");
        if (null != pageReq) {
            return Integer.valueOf(pageReq);
        } else {
            return 10;
        }
    }


    /**
     * 通过id查询数据信息
     *
     * @param id
     * @return T
     */
    T selectByPrimaryKey(M id) {
        return getService().selectByPrimaryKey(id);
    }


    /**
     * 增加数据公用方法
     *
     * @param obj
     * @return Boolean
     */
    Boolean insert(T obj) {
        return getService().insert(obj);
    }

    /**
     * 修改数据公用方法
     *
     * @param obj
     * @return Boolean
     */
    Boolean updata(T obj) {
        return getService().updata(obj);
    }




    List selectAll() {
        return getService().selectAll();
    }

    /**
     * 通过id删除数据公用方法
     *
     * @param id
     * @return Boolean
     */
    Boolean delect(M[] id) {
        return getService().delect(id);
    }

    public String arrayToStr(String[] ids) {
        StringBuffer sub = new StringBuffer();
        sub.append("[");
        for (int i = 0; i < ids.length; i++) {
            if (ids.length - 1 == i) {
                sub.append(ids[i]);
            } else {
                sub.append(ids[i] + ",");
            }
        }
        sub.append("]");
        String str = sub.toString();
        return str;
    }


}
